package cn.com.cetccst.appstore.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.lifecycle.LiveData;

import java.util.Objects;

import cn.com.cetccst.appstore.database.AppStoreRepository;
import cn.com.cetccst.appstore.database.model.AppModel;
import cn.com.cetccst.appstore.log.AppStoreLogger;
import cn.com.cetccst.appstore.manager.ApkDownloadManager;

public class PackageInstallerReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        AppStoreLogger.info("onReceive action: " + action);
        String packageName = Objects.requireNonNull(intent.getData()).getSchemeSpecificPart();
        AppModel appModel = AppStoreRepository.getInstance().getAppByPackageName(packageName);
        if (appModel != null) {
            appModel.detectAppStatus(context);
            AppStoreRepository.getInstance().saveAppModel(appModel);
        }
        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            ApkDownloadManager.getInstance().removeDownloadedApk(context, packageName);
        }
    }
}
